package cn.jinjimi.app.util;

import android.app.Application;
import android.content.Context;

import com.meituan.android.walle.ChannelInfo;
import com.meituan.android.walle.WalleChannelReader;
import cn.jinjimi.base.util.LogUtil;
import cn.jinjimi.app.AppContext;
import cn.jinjimi.app.BuildConfig;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by zhenhua on 15/11/28.
 */
public class AppUtils {

    public static final String TAG = AppUtils.class.getSimpleName();

    public static final String BD_UPDATE_SDK = "com.baidu.autoupdatesdk.BDAutoUpdateSDK";

    public static final String BD_UPDATE_METHOD = "silenceUpdateAction";

    public static void enableOneAPM(Application app) {
        try {
            Class<?> cls = Class.forName("com.oneapm.agent.android.OneApmAgent");
            Method init = cls.getDeclaredMethod("init", Application.class);
            Object oneapm = init.invoke(null, AppContext.getInstance());

            Method setToken = oneapm.getClass().getDeclaredMethod("setToken", String.class);
            setToken.invoke(oneapm, "89A1034A432A1898A0B305D4C1BDD8D062");

            Method start = oneapm.getClass().getDeclaredMethod("start", Context.class);
            start.invoke(oneapm, app);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void enableBaiduAPM(Application app) {
        try {
            Class<?> cls = Class.forName("com.baidu.uaq.agent.android.UAQ");
            Method withApplicationToken = cls.getDeclaredMethod("withApplicationToken", String.class);
            String token = null;
            Object obj = withApplicationToken.invoke(null, token);
            Method start = obj.getClass().getDeclaredMethod("start", Application.class);
            start.invoke(obj, app);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Deprecated
    public static void checkUpdate(Context context) {

        boolean BDUpdate = false;

        try {
            Class<?> clazz = Class.forName(BD_UPDATE_SDK);

            Method method = clazz.getDeclaredMethod(BD_UPDATE_METHOD, Context.class);
            //start BaiDu silenceUpdateAction;
            method.invoke(null, context);

            BDUpdate = true;

            LogUtil.d(TAG, "try to use Baidu Update");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } finally {
            if (!BDUpdate) {
//                XrkUpdateManager.checkUpdate(context,false,null);
            }
        }
    }

    public static void onLoginStateChange(Context context) {

    }

    public static String getChannel() {
        ChannelInfo channelInfo = WalleChannelReader.getChannelInfo(AppContext.getInstance());
        if (channelInfo != null) {
            return channelInfo.getChannel();
        } else {
            return BuildConfig.FLAVOR;
        }
    }
}
